package com.hydl.power.service.impl;

import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageInfo;
import com.hydl.power.mapper.MaintainerAndPowerroomMapper;
import com.hydl.power.mapper.MaintainerMapper;
import com.hydl.power.mapper.SysUserMapper;
import com.hydl.power.pojo.Maintainer;
import com.hydl.power.pojo.MaintainerAndPowerroom;
import com.hydl.power.pojo.SysUser;
import com.hydl.power.service.MaintainerService;
import com.hydl.power.utils.IdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class MaintainerServiceImpl implements MaintainerService {

    @Autowired
    private MaintainerMapper maintainerMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private MaintainerAndPowerroomMapper maintainerAndPowerroomMapper;

    @Override
    public int deleteMaintainerStatus(Integer id) {
        return maintainerMapper.deleteById(id);
    }

    @Override
    public int updateMaintainer(Integer id, Integer card, String phone) {
        Maintainer maintainer = maintainerMapper.selectById(id);
        maintainer.setCard(card);
        maintainer.setPhone(phone);
        return maintainerMapper.updateById(maintainer);
    }

    @Override
    public Maintainer selectById(Integer id) {
        return maintainerMapper.selectById(id);
    }

    @Override
    public List<Maintainer> getAll() {
        return maintainerMapper.getAll();
    }

    @Override
    public PageInfo<Maintainer> selectDevice(Integer pageNum, Integer pageSize) {
        return new PageInfo<>(maintainerMapper.getAll(pageNum,pageSize));
    }

    @Override
    public int addMaintainer(Integer card, String phone, Integer userId) {
        SysUser sysUser = sysUserMapper.getAllById(userId);
        Maintainer maintainer = new Maintainer();
        maintainer.setCard(card);
        maintainer.setPhone(phone);
        maintainer.setUserId(userId);
        maintainer.setStatus(1);
        maintainer.setName(sysUser.getUsername());
        maintainer.setMaintainerId(IdUtil.getRandomString(8));
        return maintainerMapper.insert(maintainer);
    }

    @Override
    public int addMaintainerAndPowerroom(String maintainerId, String powerroomIds) {
        List<MaintainerAndPowerroom> maintainerAndPowerrooms = maintainerAndPowerroomMapper.getList(maintainerId);
        if (maintainerAndPowerrooms.size() == 0) {
            String[] pIds = Convert.toStrArray(powerroomIds);
            List<MaintainerAndPowerroom> list = new ArrayList<>();
            for (String powerroomId : pIds) {
                MaintainerAndPowerroom maintainerAndPowerroom = new MaintainerAndPowerroom();
                maintainerAndPowerroom.setMaintainerId(maintainerId);
                maintainerAndPowerroom.setPowerroomId(powerroomId);
                maintainerAndPowerroom.setCreateTime(System.currentTimeMillis());
                list.add(maintainerAndPowerroom);
            }
            return maintainerAndPowerroomMapper.batchMaintainerAndPowerroom(list);
        } else {
            List<String> roomId = maintainerAndPowerrooms.stream().map(p -> p.getPowerroomId()).collect(Collectors.toList());
            maintainerAndPowerroomMapper.deleteMaintainerAndPowerroom(maintainerId, Convert.toStrArray(roomId));
            String[] pIds = Convert.toStrArray(powerroomIds);
            List<MaintainerAndPowerroom> list = new ArrayList<>();
            for (String powerroomId : pIds) {
                MaintainerAndPowerroom maintainerAndPowerroom = new MaintainerAndPowerroom();
                maintainerAndPowerroom.setMaintainerId(maintainerId);
                maintainerAndPowerroom.setPowerroomId(powerroomId);
                maintainerAndPowerroom.setCreateTime(System.currentTimeMillis());
                list.add(maintainerAndPowerroom);
            }
            return maintainerAndPowerroomMapper.batchMaintainerAndPowerroom(list);
        }
    }
    /*@Override
    public int deleteMaintainerAndPowerroom(String maintainerId, String powerroomIds) {
        List<MaintainerAndPowerroom> maintainerAndPowerrooms = maintainerAndPowerroomMapper.getList(maintainerId);
        List<String> roomId = maintainerAndPowerrooms.stream().map(p -> p.getPowerroomId()).collect(Collectors.toList());
        List resultList = new ArrayList();
        for(String s : roomId){
            boolean isExists = powerroomIds.contains(s);
            if(!isExists){
                resultList.add(s);
            }
        }
        return maintainerAndPowerroomMapper.deleteMaintainerAndPowerroom(maintainerId,Convert.toStrArray(resultList));
    }*/
}
